﻿// <copyright file="Tags.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "tags", Namespace = "http://search.yahoo.com/mrss/")]
    public class Tags : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "tags";

        private readonly Dictionary<string, int> tagCollection = new Dictionary<string, int>();

        #endregion

        #region Public Properties

        public Dictionary<string, int> TagCollection
        {
            get
            {
                return this.tagCollection;
            }
        }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            base.AddAttributeExtensions(reader);
            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                string str = reader.ReadContentAsString();
                if (!string.IsNullOrEmpty(str))
                {
                    string[] strArray = str.Split(new[] { ',' });
                    foreach (var str2 in strArray)
                    {
                        string[] strArray2 = str2.Split(new[] { ':' });
                        this.TagCollection.Add(strArray2[0].Trim(), int.Parse(strArray2[1].Trim()));
                    }
                }
            }
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("media", "tags", "http://search.yahoo.com/mrss/");
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            if ((this.TagCollection != null) && (this.TagCollection.Count > 0))
            {
                var builder = new StringBuilder();
                foreach (var pair2 in this.TagCollection)
                {
                    builder.Append(string.Format("{0}: {1},", pair2.Key, pair2.Value));
                }
                if (builder.Length > 0)
                {
                    writer.WriteString(builder.ToString().Remove(builder.Length - 1));
                }
            }
            writer.WriteEndElement();
        }

        #endregion
    }
}